package com.yjhb.android.sbz.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.widget.EditText;

import com.yjhb.android.sbz.App;
import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.entity.UserInfo;
import com.yjhb.android.sbz.manager.im.IMManager;
import com.yjhb.android.sbz.mvp.login.AccountLoginPresenter;
import com.yjhb.android.sbz.mvp.login.LoginView;
import com.yjhb.android.sbz.tool.ChannelUtil;
import com.yjhb.android.sbz.tool.EventMsg;
import com.yjhb.android.sbz.tool.InvitationCodeTool;
import com.yjhb.android.sbz.tool.Tool;
import com.jojo.android.zxlib.tool.Global;
import com.jojo.android.zxlib.tool.ZxToast;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者 zx
 * 创建时间 2021/2/21 10:24
 */
public class LoginAccountActivity extends BaseActivity<AccountLoginPresenter> implements LoginView {
    private final String TAG = "LoginAccountActivity";

    String invitationCode = null;
    @BindView(R.id.et_account)
    EditText etAccount;
    @BindView(R.id.et_password)
    EditText etPassword;

    @Override
    protected AccountLoginPresenter initPresenter() {
        return new AccountLoginPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login_account;
    }

    @Override
    protected void initUI() {

    }

    @Override
    protected void initData() {
        invitationCode = ChannelUtil.INSTANCE.getChannelNumber(this);
    }

    @Override
    protected void regEvent() {

    }

    @OnClick(R.id.login_btn)
    void login_btn() {
        if (TextUtils.isEmpty(Tool.getText(etAccount))) {
            ZxToast.showINFOBottomToast("请输入账号");
            return;
        }
        if (TextUtils.isEmpty(Tool.getText(etPassword))) {
            ZxToast.showINFOBottomToast("请输入密码");
            return;
        }
        presenter.login(Tool.getText(etAccount), Tool.getText(etPassword));
    }


    @Override
    public void onResume() {
        super.onResume();
        //从剪切板获取邀请码
        Global.getInstance().getMainHandler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (TextUtils.isEmpty(invitationCode))
                    invitationCode = InvitationCodeTool.getInvitationCode();
            }
        }, 200);
    }


    @Override
    public void onLoginSuccess(UserInfo userInfo) {
        loadingDialog.cancel();
        if (userInfo.getAccountType() > 2) {
            ZxToast.showINFOBottomToast("无登录权限");
            return;
        }
        App.updateUserInfo(userInfo);
        App.updateUUID(userInfo.getUuid());
        IMManager.getInstance().login();
        EventBus.getDefault().post(new EventMsg(EventMsg.LOGIN_SUCCESS,3));
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }

    @Override
    public void loggingIn() {
        loadingDialog.show();
    }

    @Override
    public void onFailure(int code, String msg) {
        loadingDialog.cancel();
        ZxToast.showCenterToastLight(msg);
    }
}
